#include "TransportLayer.h"

void TransportLayer::Exec(string mode) {
	/* 
	 * A simple implementation of TCP.
	 */
    if (mode == "UP") {
        int header_len = 0;
        for(int i = 0; i < 4; i++) {
            if (data[HEAD_IDX+i].value == 0) {
                continue;
            }
            header_len |= (1 << (3-i));
        }
        data.erase(data.begin(), data.begin() + header_len * 4 * 8);
    } else if (mode == "DOWN") {

        int header_len = 20;
        vector<bit> head_bit(header_len * 8);
        for(int i = 0; i < header_len * 8; i++) {
            head_bit[i].value = 0;
        }

        for(int i = 0; i < 16; i++) {
            head_bit[i+16].value = 1;
        }

        head_bit[HEAD_IDX+0].value = 0;
        head_bit[HEAD_IDX+1].value = 1;
        head_bit[HEAD_IDX+2].value = 0;
        head_bit[HEAD_IDX+3].value = 1;

        for(unsigned int i = 0; i < head_bit.size(); i++) {
            data.insert(data.begin() + i,head_bit[i]);
        }
    }
}

vector<bit> TransportLayer::Send() {
    return data;
}

void TransportLayer::Recv(vector<bit> b) {
    data = b;
}

void TransportLayer::SayHello() {
    cout<<"Hi, This is Transport Layer"<<endl;
}

int TransportLayer::len() {
    return data.size();
}
